import { Controller } from 'egg';

class PositionController extends Controller {
    /**
     * 创建职位
     */
    public async createPosition() {
        const { ctx, ctx: { request } } = this;
        try {
            ctx.validate({
                position_name: 'string',
                position_is_evaluate:'string',
            }, request.body)
        } catch (e) {
            ctx.body = ctx.helper.responseErrorParam(e.errors[0].message);
            return;
        }
        await ctx.service.positionService.createPosition(request.body);
        ctx.body = ctx.helper.responseSuccess('创建成功');
    }

    /**
     * 删除职位
     */
    public async deletePosition() {
        const { ctx, ctx: { request } } = this;
        try {
            ctx.validate({
                position_id: 'string',
            }, request.body)
        } catch (e) {
            ctx.body = ctx.helper.responseErrorParam(e.errors[0].message);
            return;
        }
        await ctx.service.positionService.deletePosition(request.body);
        ctx.body = ctx.helper.responseSuccess('删除成功');
    }

    /**
     * 查找职位
     */
    public async searchPosition() {
        const { ctx } = this;
        const result = await ctx.service.positionService.searchPosition();
        ctx.body = ctx.helper.responseSuccess('查找成功', result);
    }

    /**
     * 根据职位id查找职位信息
     */
    public async searchPositionById() {
        const { ctx, ctx: { request } } = this;
        try {
            ctx.validate({
                position_id: 'string'
            }, request.body)
        } catch (e) {
            ctx.body = ctx.helper.responseErrorParam(e.errors[0].message);
            return;
        }
        const result = await ctx.service.positionService.searchPositionById(request.body);
        ctx.body = ctx.helper.responseSuccess('查找成功', result);
    }

    /**
     * 修改职位
     */
    public async updatePosition() {
        const { ctx, ctx: { request } } = this;
        try {
            ctx.validate({
                admin_id:'string',
                position_id: 'string',
                position_name:'string',
                position_is_evaluate:'string',
            }, request.body)
        } catch (e) {
            ctx.body = ctx.helper.responseErrorParam(e.errors[0].message);
            return;
        }
        await ctx.service.positionService.updatePosition(request.body);
        ctx.body = ctx.helper.responseSuccess('修改成功');
    }
}
export default PositionController;